package com.bolehui.base.utils;

import com.bolehui.domain.po.UserPO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName SecurityUtil
 * @Description
 * @createTime 2024/12/13 10:46
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
public class SecurityUtil {

    public static UserPO getCurrentUserInfo() throws ClassCastException {
        Authentication auth = (Authentication) SecurityContextHolder.getContext().getAuthentication();
        if (auth != null && auth.getPrincipal() instanceof UserPO) {
            return (UserPO) auth.getPrincipal();
        } else {
            return null;
        }
    }

    public static Long getCurrentUserId() {
        UserPO userPO = getCurrentUserInfo();
        return userPO == null ? null : userPO.getUserId();
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }
}
